import os
import socket
import json
import struct
#发送
sk=socket.socket()
sk.connect(('192.168.56.1',9001))

#文件名/文件大小
abs_path=r'D:\AK\网络编程\tcp协议的文件传输\tmp'
filename=os.path.basename(abs_path)
filesize=os.path.getsize(abs_path)
dic={'filename':filename,'filesize':filesize}
str_dic=json.dumps(dic)
b_dic=str_dic.encode('utf-8')
mlen=struct.pack('i',len(b_dic))
sk.send(mlen)#四个字节 表示字典转换成字节后的长度
sk.send(b_dic)#具体的字典数据
#读取小文件
# with open(abs_path,mode='rb') as f:
#     content=f.read()
#     sk.send(content)

#读取大文件
with open(abs_path,mode='rb') as f:
    while filesize>0:
        content=f.read(1024)
        # filesize-=1024
        filesize-=len(content)#最好这样写
        sk.send(content)


sk.close()





















